<?php

namespace App\Http\Controllers\Quiz\Option;

use App\Contracts\Repositories\Quiz\OptionRepository as OptionRepositoryContract;
use App\Contracts\Services\Storage\StorageService as StorageServiceContract;
use App\Http\Controllers\Controller;
use App\Http\Responses\Code;
use App\Morefun\Quiz\OptionService;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;

class ApiController extends Controller
{
    protected $options;
    protected $storage;

    public function __construct(OptionRepositoryContract $options, StorageServiceContract $storage)
    {
        $this->options = $options;
        $this->storage = $storage;
    }

    public function store(Request $request, $id)
    {
        $input = $request->json()->all();
        $input = $this->checkOption($input);
        Arr::set($input, 'quiz_id', $id);
        if ($id = $this->options->create($input)) {
            return response()->success(compact('id'));
        }

        return response()->failed('选项添加失败');
    }

    public function multipleUpdateOptions(Request $request, OptionService $option, $id)
    {
        $inputs = $request->json()->all();
        $update = [];
        $insert = [];
        if ($inputs) {
            foreach ($inputs as $input) {
                $input = $this->checkOption($input);
                if (isset($input['id'])) {
                    unset($input['id']);
                }
                Arr::set($input, 'quiz_id', $id);
                $insert[] = $input;
            }
        }
        $result = $option->updateQuizOptions($id, $insert);
        if ($result) {
            return response()->success($result);
        }

        return response()->failed('选项添加失败');
    }

    private function checkOption($option)
    {
//        $validator = Validator::make($option, [
//            'text'          => 'required|max:255',
//            'fillable'      => 'required|boolean',
//            'fill_required' => 'required_if:fillable,1',
//            'sort'          => 'integer',
//        ]);
        $validator = Validator::make($option, [
            'text'          => 'required|max:255',
            'fillable'      => 'required|boolean',
            'fill_required' => 'required|boolean',
        ]);
        $option['resource'] = empty($option['resource']) ? '' : $option['resource'];

        if ($validator->fails()) {
            throw new \Exception(Arr::first($validator->errors()->all()));
        }

        return $option;
    }

    public function many($id)
    {
        return response()->success(
            $this->options->findByQuizId($id)
        );
    }

    public function show($id)
    {
        $option = $this->options->find($id);

        if (null === $option) {
            return response()->failed('没有找到资源', Code::NOT_FOUND);
        }

        $option->resource = $this->storage->toHttpUrl($option->resource);

        return response()->success($option);
    }

    public function update(Request $request, $id)
    {
        $input = $request->json()->all();
        $input = $this->checkOption($input);
        if ($this->options->update($id, $input)) {
            return response()->success();
        }

        return response()->failed();
    }

    public function destroy($id)
    {
        if ($this->options->delete($id)) {
            return response()->success();
        }

        return response()->failed('删除失败');
    }
}
